﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EjotyI
{
    class Program
    {
        static void Main(string[] args)
        {
            bool loopon;            //the loop initializer
            StreamReader myStr;     //the IO tool
            string address;     //where the file is located
            string text;        //to hold the entire file's contents
            string[] input;     //the array holding individual commands
            int a = 0;          //file command reader counter, a
            int loopstart;      //holds the place where the loop started

            Console.WriteLine("Welcome, This Program interprets files written in Ejoty 0.8");
            for (loopon = true; loopon == true; )   //main loop
            {
                Console.WriteLine("Enter the path of the file you wish to interpret:");
                address = Console.ReadLine();   //store the address of the file
                if (File.Exists(address))       //check to make sure file exists
                {
                    Console.WriteLine("File Found");    //tell user that the file exists
                    myStr = File.OpenText(address);     //open access to the file
                    text = myStr.ReadToEnd();           //read the contents of the file and store in text
                    char delimiters = '/';              //seperate the indivdual commands using "/"
                    input = text.Split(delimiters);     //split text into individual commands, store in input
                    Computer comp = new Computer();     //declare object refrence
                    for (a = 0; a < input.Length; a++)      //begin the interpitation
                    {
                        if (input[a] == "{")                //check if the command the branch statement
                        {
                            if (comp.pointer[comp.c] == comp.pointer[comp.n])   //run the branch statement
                            {
                                a++;
                                for (; input[a] != "}"; a++)        //if branch true, run the code
                                {
                                    comp.EjotyS(input[a]);
                                }
                            }
                            else          //if not true, ignore the code
                            {
                                a++;
                                for (; input[a] != "}"; a++)
                                {
                                    //do nothing
                                }
                            }
                        }
                        else if (input[a] == "(")
                        {
                            loopstart = a;      //store the location of the start of loop for reference
                            for (; ; a++)
                            {
                                if (input[a] == ")")
                                {
                                    a = loopstart;      //if the end of loop is reached, go back to the start
                                }
                                comp.EjotyS(input[a]);      //interpret the loops contents
                            }
                        }
                        else if (input[a] == "@")       //The comment symbol
                        {
                            for (; input[a] != "@"; a++)
                            {
                                //do nothing
                            }
                        }
                        else        // if not the branch statement or loop statement, run the regular statements
                        {
                            comp.EjotyS(input[a]);
                        }
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Could not find file.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
        }
    }
    //class computer handles all non complex commands and the virtualization of a turing machine
    class Computer
    {
        public int n = 0;          //Counter n
        public int c = 0;          //Counter c
        bool pointerNOn = false;     //used to switch the pointer
        public int[] pointer = new int[32]; //used to hold the cells value
        int b;                  //Holds the currently used pointer
        public void EjotyS(string szInput)
        {
            switch (szInput)
            {
                case "+":
                    if (pointerNOn == true)
                    {
                        b = n;
                    }
                    else
                    {
                        b = c;
                    }
                    pointer[b]++;
                    break;
                case "-":
                    if (pointerNOn == true)
                    {
                        b = n;
                    }
                    else
                    {
                        b = c;
                    }
                    pointer[b]--;
                    break;
                case ">":
                    if (pointerNOn == true)
                    {
                        n++;
                    }
                    else
                    {
                        c++;
                    }
                    break;
                case "<":
                    if (pointerNOn == true)
                    {
                        n--;
                    }
                    else
                    {
                        c--;
                    }
                    break;
                case "c":
                    pointerNOn = false;
                    break;
                case "n":
                    pointerNOn = true;
                    break;
                case ".":
                    if (pointerNOn == true)
                    {
                        b = n;
                    }
                    else
                    {
                        b = c;
                    }
                    char text1 = Ejoty.EjotyIO.IntToString(pointer[b]);
                    Console.WriteLine();
                    Console.WriteLine(text1);
                    break;
                case ";":
                    Console.WriteLine();
                    char text3;
                    int q;
                    for (q = 0; q < 32; q++)
                    {
                        text3 = Ejoty.EjotyIO.IntToString(pointer[q]);
                        Console.Write(text3);
                    }
                    Console.WriteLine();
                    break;
                case ",":
                    Console.WriteLine();
                    string text2 = Console.ReadLine();
                    if (pointerNOn == true)
                    {
                        b = n;
                    }
                    else
                    {
                        b = c;
                    }
                    pointer[b] = Ejoty.EjotyIO.CharToInt(text2);
                    break;
            }
        }
    }
}
